package com.sunday.common.netty.e8_Handlerorder.server.inbound;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class InBoundHandler2 extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("[In][2][Into] InBoundHandler.channelRead");

        String data = ((ByteBuf) msg).toString(CharsetUtil.UTF_8);
        log.info("[In][2][receive] InBoundHandler.channelRead receive: " + data);
        // If do not call fireChannelRead, the next ChannelHandler will not exec.
        ctx.fireChannelRead(msg);
        log.info("[In][2][Exit] InBoundHandler channelRead");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        log.info("[In][2][readComplete] InBoundHandler readComplete.");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.info("[In][2][exceptionCaught] InBoundHandler exceptionCaught.");
    }
}
